package com.xmfh.wemedia.util;

import lombok.Data;

@Data
public class JsonResult<T> {
    private Integer state;
    private String message;
    private T data;

    public JsonResult() {
    }

    public static JsonResult<Void> ok() {
        return ok(null);
    }

    public static <T> JsonResult<T> ok(T data) {
        JsonResult<T> jsonResult = new JsonResult<>();
        jsonResult.state = State.OK.getValue();
        jsonResult.data = data;
        return jsonResult;
    }

    public static JsonResult<Void> fail(State state, String message) {
        JsonResult<Void> jsonResult = new JsonResult<>();
        jsonResult.state = state.getValue();
        jsonResult.message = message;
        return jsonResult;
    }

    public enum State {
        OK(200),
        /*用户错误提示代码*/
        USER_EXISTS(5001),
        USER_NULL(5002),
        ERROR_PASSWORD(6000),
        /*上传错误代码*/
        EMPTY_FILE(7000),
        ERROR_UPLOAD(7001),
        /*未知错误代码*/
        DATABASE_ERROR(8000),
        /*文件错误*/
        FILE_ERROR(9000),
        ERROR_UNKNOWN(9999);

        Integer value;

        State(Integer value) {
            this.value = value;
        }

        public Integer getValue() {
            return value;
        }
    }
}
